<?php
/**
 * Class Action of your Home page
 * 
 * @author SillySmart
 * @copyright SillySmart
 * @package Mvc.Controllers.Home
 * @see Mvc.Controllers.Home.ControllerProtected
 * @see Mvc.Controllers.SiteProtected
 * @see Sls.Controllers.Core.SLS_GenericController 
 */
class HomeIndex extends HomeControllerProtected 
{
	public function init()
	{
		parent::init();
	}
	
	/**
	 * @access public
	 */
	public function action()
	{
		$qbd = new SLS_QueryBuilder();
		$xml = $this->getXml();
		$lang = $this->_lang->getLang();

		# Projects
		$xml->startTag('projects');
		$projects = $qbd->select('*')
			->from('project')
			->where($qbd->expr()->eq('pk_lang', $lang))
			->order('project_id', 'desc')
			->execute();

		foreach($projects as $project)
		{
			$xml->startTag('project');
				$xml->addFullTag('project_id', $project->project_id, true);
				$xml->addFullTag('project_name', $project->project_name, true);
				$xml->addFullTag('project_home_desktop_img', SLS_String::getUrlFile($project->project_home_desktop_img), true);
				$xml->addFullTag('project_home_tablet_img', SLS_String::getUrlFile($project->project_home_tablet_img), true);
				$xml->addFullTag('project_url', $this->_generic->getFullPath('Home', 'Project', array(array('key' => 'Id', 'value' => $project->project_id))), true);
			$xml->endTag('project');
		}
		$xml->endTag('projects');
		# /Projects

		# Quotes
		$xml->startTag('quotes');
		$quotes = $qbd->select('*')
			->from('quote')
			->where($qbd->expr()->eq('pk_lang', $lang))
			->orderRand()
			->execute();

		foreach($quotes as $quote)
		{
			$xml->startTag('quote');
				foreach($quote as $key => $value)
				{
					if($key == 'quote_content')
						$value = nl2br($value);
					$xml->addFullTag($key, $value, true);
				}

			$xml->endTag('quote');
		}
		$xml->endTag('quotes');
		# /Quotes

		# Instagram
		$xml->startTag('instagram');
		$instagram = new Instagram($this->_generic->getFullPath('Home', 'Index'));
		$instagram->setAccessToken('232091229.c240af8.54046507f9cc4373ae1ea3bd358a8749');
		$instagramId = 232091229;
		$instagramItems = $instagram->getUserMedia($instagramId, 30);

		if($instagramItems->meta->code == 200)
		{
			$medias = $instagramItems->data;
			shuffle($medias);
			foreach($medias as $media)
			{
				$xml->startTag('item');
					$xml->addFullTag('item_content', SLS_String::trimStringToLength($media->caption->text, 90), true);
					$xml->addFullTag('item_img', $media->images->standard_resolution->url, true);
				$xml->endTag('item');
			}
		}
		$xml->endTag('instagram');
		# /Instagram

		# Home slider
		$xml->startTag('home_slider');
		$homeSliderItems = $qbd->select('*')
			->from('home_slider')
			->where($qbd->expr()->eq('pk_lang', $lang))
			->order('home_slider_id', 'desc')
			->execute();

		foreach($homeSliderItems as $homeSliderItem)
		{
			$xml->startTag('home_slider_item');
			foreach($homeSliderItem as $key => $value)
			{
				if($key == 'home_slider_img')
					$value = SLS_String::getUrlFile($value);
				$xml->addFullTag($key, $value, true);
			}
			$xml->endTag('home_slider_item');
		}
		$xml->endTag('home_slider');
		# /Home slider

		# Nav Projects
		$xml->startTag('nav_projects');
		$projects = $qbd->select('*')
			->from('project')
			->where($qbd->expr()->eq('pk_lang', $lang))
			->order('project_id', 'desc')
			->execute();

		foreach($projects as $project)
		{
			$xml->startTag('project');
				$xml->addFullTag('project_id', $project->project_id, true);
			$xml->endTag('project');
		}
		$xml->endTag('nav_projects');
		# /Nav Projects

		$this->saveXml($xml);
	}
}
?>